/*
 * Copyright (C) 2005, Velocityme Partnership
 * LicenceManagementActions.java
 *
 * Created on 18 June 2005, 02:22
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.session.KeyLocal;
import com.velocityme.session.LicenceKeyLocal;
import com.velocityme.session.ServerConfigurationLocal;
import com.velocityme.utility.Licence;
import com.velocityme.www.actionforms.LicenceManagementActionForm;
import com.velocityme.www.dto.LicenceKeyDTO;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class LicenceManagementActions extends NodeActions {
    
    /** Creates a new instance of LicenceManagementActions */
    public LicenceManagementActions() {
    }
    
    @Override
    public ActionForward show(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        request.setAttribute("selectedTab", "LicenceManagement");
        return super.show(mapping, form, request, response);
    }

    public ActionForward showNode(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Collection<Licence> licences = lookupLicenceKeyBean().findAllLicences(key);
        Collection<LicenceKeyDTO> keyDTOs = new ArrayList<LicenceKeyDTO>();
        Date now = new Date();
        for (Licence licence: licences) {
            boolean expired = licence.getExpiryDate().before(now);
            LicenceKeyDTO dto = new LicenceKeyDTO(expired, licence.getCreationDate(), licence.getExpiryDate(), licence.toString());
            keyDTOs.add(dto);
        }
        session.setAttribute("licenceKeys", keyDTOs);
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showAddLicence(ActionMapping mapping, 
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        session.setAttribute("clientGUID", lookupServerConfigurationBean().getClientGUID());
        return mapping.findForward("ACTION_ADD_LICENCE");                                      
    }
    
    public ActionForward addLicence(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        LicenceManagementActionForm actionForm = (LicenceManagementActionForm)form;
        if (request.getParameter("btnSubmit").equals("OK")) {
            lookupLicenceKeyBean().addEncodedLicence(actionForm.getEncodedLicence());
        }
        return mapping.findForward("ACTION_SHOW_NODE");
    }
    
    public ActionForward installLicence(ActionMapping mapping,
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        LicenceManagementActionForm actionForm = (LicenceManagementActionForm)form;
        lookupLicenceKeyBean().addEncodedLicence(actionForm.getEncodedLicence());
        return mapping.findForward("ACTION_SHOW_LOGIN");
    }

    private ServerConfigurationLocal lookupServerConfigurationBean() {
        try {
            Context c = new InitialContext();
            return (ServerConfigurationLocal) c.lookup("java:comp/env/ServerConfigurationBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private LicenceKeyLocal lookupLicenceKeyBean() {
        try {
            Context c = new InitialContext();
            return (LicenceKeyLocal) c.lookup("java:comp/env/LicenceKeyBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    

    
}
